#!/bin/bash

# 获取环境变量
PLAY_HOME=/opt/playground

# 配置fe.conf
function configureFe()
{
 FeConf=$1
 SUBNET=$2

# 检查配置文件是否存在
if [ -f "$FeConf" ]; then
    # 使用sed命令在特定注释后面追加新的配置行
    sed -i "/# priority_networks = 10.10.10.0\/24;192.168.0.0\/16/a\\priority_networks = ${SUBNET}" $FeConf
else
    echo "配置文件 $FeConf 不存在"
fi
}

# 配置be.conf
function configureBe()
{
 BeConf=$1
 SUBNET=$2
 
 # 检查配置文件是否存在
 if [ -f "$BeConf" ]; then
     # 使用sed命令在特定注释后面追加新的配置行
     sed -i "/# priority_networks = 10.10.10.0\/24;192.168.0.0\/16/a\\priority_networks = ${SUBNET}" $BeConf
 else
     echo "配置文件 $BeConf 不存在"
 fi
}

function installStarRocks()
{
 #1.在frames.txt中查看是否需要安装starrocks
 starrocksInfo=`egrep "^StarRocks" $PLAY_HOME/frames.txt`

 starrocks=`echo $starrocksInfo | cut -d " " -f1`
 isInstall=`echo $starrocksInfo | cut -d " " -f2`
 starrocksNodes=`echo $starrocksInfo | cut -d " " -f3` 
 masterNode=`echo $starrocksInfo | cut -d " " -f4` 
 node=`hostname`
 

 #是否安装
 if [[ $isInstall = "true" && $starrocksNodes =~ $node ]];then
    
     #2.查看$PLAY_HOME/frames目录下是否有starrocks安装包
     starrocksIsExists=`find $PLAY_HOME/frames -name $starrocks`

     if [[ ${#starrocksIsExists} -ne 0 ]];then

          if [[ ! -d /opt/app ]];then
              mkdir /opt/app && chmod -R 775 /opt/app
          fi

          #删除旧的
          starrocks_home_old=`find /opt/app -maxdepth 1 -name "StarRocks*"`
          for i in $starrocks_home_old;do
                rm -rf $i
          done

          #3.解压到指定文件夹/opt/app中
          echo "开始解压starrocks安装包"
          tar -zxvf $starrocksIsExists -C /opt/app >& /dev/null
          echo "starrocks安装包解压完毕"

          starrocks_home=`find /opt/app -maxdepth 1 -name "StarRocks*"`
          
          # 创建FE的meta目录
          echo "创建FE的meta目录"
          mkdir -p $starrocks_home/fe/meta
          echo "FE的meta目录创建完毕"

          # 获取hostname
         HOSTNAME=`hostname`
 
         # 从/etc/hosts文件中获取IP地址
         IP_ADDRESS=$(grep -w $HOSTNAME /etc/hosts | awk '{print $1}')
 
         # 检查是否获取到了IP地址
         if [ -z "$IP_ADDRESS" ]; then
             echo "无法从/etc/hosts获取IP地址"
             exit 1
         fi
 
         # 提取IP地址的前三个部分，并构建子网
         SUBNET=$(echo $IP_ADDRESS | cut -d '.' -f 1-3 | sed 's/ /\\./g')'.0/16'

          #5.配置fe文件
          configureFe $starrocks_home/fe/conf/fe.conf $SUBNET

          #6.配置be文件
          configureBe $starrocks_home/be/conf/be.conf $SUBNET

          #7.配置STARROCKS_HOME
          profile=/etc/profile
          sed -i "/^export STARROCKS_HOME/d" $profile
          echo "export STARROCKS_HOME=$starrocks_home" >> $profile

          #8.配置BE PATH
          sed -i "/^export PATH=\$PATH:\$STARROCKS_HOME\/be\/bin/d" $profile
          echo "export PATH=\$PATH:\$STARROCKS_HOME/be/bin" >> $profile

          #9.配置FE PATH
          sed -i "/^export PATH=\$PATH:\$STARROCKS_HOME\/fe\/bin/d" $profile
          echo "export PATH=\$PATH:\$STARROCKS_HOME/fe/bin" >> $profile

          #10.更新/etc/profile文件
          source /etc/profile && source /etc/profile
     else
         echo "$PLAY_HOME/frames目录下没有starrocks安装包"
     fi
 else
     echo "StarRocks不允许安装在当前节点，请检查配置文件！"
 fi

}

installStarRocks